﻿#region Directives

using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
using System.Collections;

#endregion Directives

namespace ConnectIntegrationHub.WebService.Common
{
    public class Strings
    {
        #region Constants

        public static readonly String[] alphaLetters = new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ", "BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL", "BM", "BN", "BO", "BP", "BQ", "BR", "BS", "BT", "BU", "BV", "BW", "BX", "BY", "BZ" };
        public static readonly String specialCharacter_DoubleQuote = '"'.ToString(CultureInfo.CurrentCulture);

        public const String specialCharacter_SingleQuote = "'";
        public const String specialCharacter_Tab = "\t";
        public const String specialCharacter_Ampersand = "&";
        public const String specialCharacter_And = "And";
        public const String specialCharacter_Carriage_Return = "\r";
        
        public const String paddingSpace = " ";
        public const String zeroCharacter = "0";
        public const String paddingDash = " - ";
        
        public const Char specialCharacter_Comma = ',';
        
        #endregion Constants

        #region Public Static Methods
        
        public static Byte[] getBinary(String text)
        {
            Byte[] result = null;
            if (!String.IsNullOrEmpty(text))
            {
                result = System.Text.Encoding.Default.GetBytes(text.Trim());
            }
            return result;
        }

        public static String getBinaryString(Byte[] aBinary)
        {
            if (aBinary != null)
            {
                return System.Text.Encoding.Default.GetString(aBinary, 0, aBinary.Length).Trim();
            }
            return String.Empty;
        }

        public static String cleanText(String aText)
        {
            if (aText.IsNullOrEmpty())
            {
                return String.Empty;
            }

            String result = String.Empty;
            foreach (Char c in aText.ToCharArray())
            {
                if (Char.IsWhiteSpace(c))
                {
                    result += " ";
                }
                else
                {
                    result += c.ToString(CultureInfo.CurrentCulture);
                }
            }
            return result;
        }

        /// <summary>
        /// Gets letters from a string.
        /// </summary>
        /// <param name="aString">String</param>
        /// <returns>Letter String</returns>
        public static String getLetters(String aString)
        {
            String result = String.Empty;
            if (!aString.IsNullOrEmpty())
            {
                foreach (Char c in aString.ToCharArray())
                {
                    if (Char.IsLetter(c))
                    {
                        result += c.ToString(CultureInfo.CurrentCulture);
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// Gets numbers from a string.
        /// </summary>
        /// <param name="aString">String</param>
        /// <returns>Number String</returns>
        public static String getNumbers(String aString)
        {
            String result = String.Empty;
            if (!aString.IsNullOrEmpty())
            {
                foreach (Char c in aString.ToCharArray())
                {
                    if (Char.IsNumber(c))
                    {
                        result += c.ToString(CultureInfo.CurrentCulture);
                    }
                }

            }
            return result;
        }

        /// <summary>
        /// Strips all HTML content out of string
        /// </summary>
        /// <param name="text">String to Strip.</param>
        /// <returns>String without HTML elements.</returns>
        public static String stripHTML(String text)
        {
            if (text.IsNullOrEmpty())
            {
                return String.Empty;
            }
            else
            {
                return
                    System.Text.RegularExpressions.Regex.Replace(
                        text,
                        @"<(.|\n)*?>",
                        String.Empty
                    ).Replace(
                        "&nbsp;",
                        " "
                    );
            }
        }
        
        public static List<String> getValuesFromText(String text, String startIndex, String endIndex)
        {
            List<String> result = new List<String>();

            String tempData = text;
            String sValue = String.Empty;
            Int32 iValueIndex = 1;

            Int32 iStart = 0;
            Int32 iEnd = 0;

            iStart = 0;
            while (iStart != -1)
            {
                iStart = tempData.IndexOf(startIndex, StringComparison.OrdinalIgnoreCase);
                if (iStart == -1)
                {
                    break;
                }
                tempData = tempData.Substring(iStart + startIndex.Length);
                iEnd = tempData.IndexOf(endIndex, StringComparison.OrdinalIgnoreCase);

                result.Add(tempData.Substring(0, iEnd));

                iValueIndex++;
            }
            return result;
        }

        public static String concat(ICollection items, String delimiter)
        {
            Boolean first = true;

            StringBuilder sb = new StringBuilder();
            foreach (object item in items)
            {
                if (item == null)
                {
                    continue;
                }                    

                if (!first)
                {
                    sb.Append(delimiter);
                }
                else
                {
                    first = false;
                }
                sb.Append(item);
            }
            return sb.ToString();
        }

        public static String getPaddedLeftString(String text, String paddingString, Int32 length)
        {
            if (text.IsNullOrEmpty())
            {
                text = String.Empty;
            }

            String result = text;
            if (result.Length < length)
            {
                while (result.Length < length)
                {
                    result = paddingString + result ;
                }
            }
            else
            {
                result = result.Substring(0, length);
            }
            return result;
        }

        public static String getPaddedRightString(String text, String paddingString, Int32 length)
        {
            if (text.IsNullOrEmpty())
            {
                text = String.Empty;
            }

            String result = text;
            if (result.Length < length)
            {
                while (result.Length < length)
                {
                    result = result + paddingString;
                }
            }
            else
            {
                result = result.Substring(0, length);
            }            
            return result;
        }

        #endregion Public Static Methods
    }
}